#Locking down violence 10.18.22
#Replication file 
#Appendix join-counts
####################################################################################
#Packages
library(tidyverse)
library("spdep") 
library("raster")

####################################################################################
#Tests of spatial clustering of violent events before and during lockdowns

load(file= "nolockdown.spdf", verbose = FALSE)
nolockdown.spdf <- nolockdown
coordinates(nolockdown.spdf) <- c("longitude", "latitude")
load(file= "lockdown.spdf", verbose = FALSE)
lockdown.spdf <- lockdown
coordinates(lockdown.spdf) <- c("longitude", "latitude")
#Define the extent for the join count analyses for lockdown
jc.extent <- extent(39,46,30,40)
#Set cell size
r <- raster(nrows=25, ncols=20, ext=jc.extent)

####################################################################################
#rook
####################################################################################
#Join Count Analysis for NO LOCKDOWN
nolockdown.rast <- rasterize(nolockdown.spdf, r, field = 1)
nolockdown.rast[is.na(nolockdown.rast)] <- 0
nbnl <- cell2nb(nrow = nrow(nolockdown.rast), ncol = ncol(nolockdown.rast))
# Convert the neighbors list to a 'weights' list; 
lwbnl <- nb2listw(nbnl, style = "B")
joincount.test(as.factor(nolockdown.rast@data@values), lwbnl, alternative = "greater")

#Join Count Analysis for LOCKDOWN
lockdown.rast <- rasterize(lockdown.spdf, r, field = 1)
lockdown.rast[is.na(lockdown.rast)] <- 0
nbnl <- cell2nb(nrow = nrow(lockdown.rast), ncol = ncol(lockdown.rast))
# Convert the neighbors list to a 'weights' list; 
lwbnl <- nb2listw(nbnl, style = "B")
joincount.test(as.factor(lockdown.rast@data@values), lwbnl, alternative = "greater")

####################################################################################
#queen
####################################################################################

nbnl.queen <- cell2nb(nrow = nrow(nolockdown.rast), ncol = ncol(nolockdown.rast), type = "queen")
lwbnl.queen <- nb2listw(nbnl.queen, style = "B")
joincount.test(as.factor(nolockdown.rast@data@values), lwbnl.queen, alternative = "greater")

nbl.queen <- cell2nb(nrow = nrow(lockdown.rast), ncol = ncol(lockdown.rast), type = "queen")
lwbl.queen <- nb2listw(nbl.queen, style = "B")
joincount.test(as.factor(lockdown.rast@data@values), lwbl.queen, alternative = "greater")

####################################################################################
#VARYING CELL SIZE (rook)
####################################################################################

r1 <- raster(nrows=12.5, ncols=12.5, ext=jc.extent)

#Join Count Analysis for NO LOCKDOWN
nolockdown.rast <- rasterize(nolockdown.spdf, r1, field = 1)
nolockdown.rast[is.na(nolockdown.rast)] <- 0
nbnl <- cell2nb(nrow = nrow(nolockdown.rast), ncol = ncol(nolockdown.rast))
# Convert the neighbors list to a 'weights' list; 
lwbnl <- nb2listw(nbnl, style = "B")
joincount.test(as.factor(nolockdown.rast@data@values), lwbnl, alternative = "greater")

#Join Count Analysis for LOCKDOWN
lockdown.rast <- rasterize(lockdown.spdf, r1, field = 1)
lockdown.rast[is.na(lockdown.rast)] <- 0
nbnl <- cell2nb(nrow = nrow(lockdown.rast), ncol = ncol(lockdown.rast))
# Convert the neighbors list to a 'weights' list; 
lwbnl <- nb2listw(nbnl, style = "B")
joincount.test(as.factor(lockdown.rast@data@values), lwbnl, alternative = "greater")



